package com.example.demo.exception;

import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author Administrator
 */
@Slf4j
@RestControllerAdvice
public class CustomExtHandler {



    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request) {
        String errorStr = e.getMessage();
        log.error("url {}, msg {}", request.getRequestURL(), errorStr);
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("code", 500);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    @ExceptionHandler(value = RuntimeException.class)
    Object handleBusinessException(RuntimeException e, HttpServletRequest request) {
        log.error("url {}, msg {}", request.getRequestURL(), e.getMessage());
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("code", 501);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    @ExceptionHandler(value = BusinessException.class)
    Object handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.error("url {}, msg {}", request.getRequestURL(), e.getMessage());
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("code", e.getCode());
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }
}
